package java;

import java.util.Arrays;

public class Question6 {
    public static void main(String[] args) {
        String s = "PAYPALISHIRING";
        int numRows = 3;
        System.out.println(convert(s,numRows));
        System.out.println(convert2(s,numRows));

    }

    private static String convert2(String s, int numRows) {
        if (s == null || s.length() < 1 || numRows == 1){
            return s;
        }
        if(numRows < 1){
            return null;
        }
        boolean flag = true;
        char[][] arr = new char[numRows][s.length()];
        int index = 0;
        String res = "";
        for (int j = 0; j < s.length() ;j ++){
            if (flag){
                for (int i = 0; i < numRows ; i++){
                    if (index < s.length()){
                        arr[i][j] = s.charAt(index++);
                    }else {
                        return output(arr);
                    }
                }
                flag = false;
            }else {
                for (int i = numRows - 2 ; i > 0; i --){
                    if (index < s.length()){
                        arr[i][j] = s.charAt(index++);
                    }else {
                        return output(arr);
                    }
                    j++;
                }
                j --;
                flag = true;
            }
        }
        return output(arr);
    }

    private static String output(char[][] arr) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0;i < arr.length; i ++){
            for (int j = 0;j < arr[0].length ; j ++){
                if (arr[i][j] != '\u0000'){
                    stringBuffer.append(arr[i][j]);
                }
            }
        }
        return stringBuffer.toString();
    }

    private static String convert(String s, int numRows) {
        if (s == null || s.length() < 1 || numRows == 1){
            return s;
        }
        if(numRows < 1){
            return null;
        }
        StringBuffer stringBuffer = new StringBuffer();
        StringBuffer resBuffer = new StringBuffer();
        int length = s.length();
        if(numRows == 2){
            int times = length / numRows;
            for (int i = 0;i < 2 ; i ++){
                for (int j = 0;j <= times;j ++){
                    if (i + numRows * j < length){
                        resBuffer.append(s.charAt(i + numRows * j));
                    }
                }

            }
            return resBuffer.toString();
        }

        int count = 0;
        for (int i = 0; i < length ; i ++){
            stringBuffer.append(s.charAt(i));
            if (count == numRows - 1 || count == numRows + numRows - 3){
                stringBuffer.append(' ');
            }
            count ++;
            if (count > numRows + numRows - 3){
                count = 0;
            }
        }
        int lengthOfBuffer = stringBuffer.length();
        int times = lengthOfBuffer / (2 * numRows);

        for (int i = 0; i < numRows ; i++){
            for (int j = 0; j <= times ; j ++){
                if (i + 2 * numRows * j < lengthOfBuffer){
                    resBuffer.append(stringBuffer.charAt(i + 2 * numRows * j ));
                    if (2 * numRows * j + numRows - i < lengthOfBuffer
                            && 2 * numRows * j + 2 * numRows - 1 - i < lengthOfBuffer
                            && stringBuffer.charAt(2 * numRows * j + 2 * numRows - 1 - i) != ' '){
                        resBuffer.append(stringBuffer.charAt(2 * numRows * j + 2 * numRows - 1 - i));
                    }
                }
            }
        }
        return resBuffer.toString();
    }
}
